package io.xxx.walle.assistant;

import io.xxx.walle.exception.NoTokenException;
import io.xxx.walle.wechaty.RobotClient;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Optional;

@Service
public class TokenService {

    @Resource
    private TokenRepository tokenRepository;

    @Resource
    private RobotClient robotClient;

    public void createRobot() throws IOException {
        Optional<Token> token = tokenRepository.findByUsed(false);
        if (token.isEmpty()) {
            throw new NoTokenException();
        }
        robotClient.create(token.get());
    }

    public void updateQrcode(Token token) {
        tokenRepository.updateQrcode(token.getId(), token.getQrcode());
    }

    public void useToken(Token token) {
        token.setUsed(true);
        tokenRepository.save(token);
    }
}
